package com.itliang.web.controller.system;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itliang.annotation.Log;
import com.itliang.core.model.DeleteRequest;
import com.itliang.core.model.IdRequest;
import com.itliang.core.model.dto.system.sysrole.AssignRoleRequest;
import com.itliang.core.model.dto.system.sysrole.SysRoleAddRequest;
import com.itliang.core.model.dto.system.sysrole.SysRoleQueryRequest;
import com.itliang.core.model.dto.system.sysrole.SysRoleUpdateRequest;
import com.itliang.core.model.entity.system.SysRole;
import com.itliang.core.result.BaseResponse;
import com.itliang.core.result.CodeEnum;
import com.itliang.core.result.ResultUtils;
import com.itliang.enums.BusinessType;
import com.itliang.exception.BusinessException;
import com.itliang.service.system.SysRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author : liangzhuoyuan
 */
@Api(tags = "角色管理接口")
@RestController
@RequestMapping("/admin/system/sysRole")
public class SysRoleController {

    @Resource
    SysRoleService sysRoleService;

    /**
     * 根据用户获取角色数据
     *
     * @param idRequest
     * @return
     */
    @Log(title = "角色管理", businessType = BusinessType.ASSIGN)
    @ApiOperation(value = "根据用户获取角色数据")
    @GetMapping("/toAssign")
    public BaseResponse<Map> toAssign(IdRequest idRequest) {
        Map<String, Object> roleMap = sysRoleService.getRolesByUserId(idRequest);
        return ResultUtils.success(roleMap);
    }

    /**
     * 根据用户分配角色
     *
     * @param assignRoleRequest
     * @return
     */
    @Log(title = "角色管理", businessType = BusinessType.ASSIGN)
    @ApiOperation(value = "根据用户分配角色")
    @PostMapping("/doAssign")
    public BaseResponse<Long> doAssign(@RequestBody AssignRoleRequest assignRoleRequest) {
        sysRoleService.doAssign(assignRoleRequest);
        return ResultUtils.success();
    }

    // region 增删改查

    /**
     * 创建角色
     *
     * @param sysRoleAddRequest
     * @return
     */
    @Log(title = "角色管理", businessType = BusinessType.INSERT)
    @PreAuthorize("hasAuthority('btn.sysRole.add')")
    @PostMapping("/add")
    public BaseResponse<Long> addRole(@RequestBody SysRoleAddRequest sysRoleAddRequest) {
        if (sysRoleAddRequest == null) {
            throw new BusinessException(CodeEnum.PARAMS_ERROR);
        }
        SysRole sysRole = new SysRole();
        BeanUtils.copyProperties(sysRoleAddRequest, sysRole);
        boolean result = sysRoleService.save(sysRole);
        if (!result) {
            throw new BusinessException(CodeEnum.OPERATION_ERROR);
        }
        return ResultUtils.success(sysRole.getId(), "添加成功");
    }

    /**
     * 删除角色
     *
     * @param deleteRequest
     * @return
     */
    @Log(title = "角色管理", businessType = BusinessType.DELETE)
    @PreAuthorize("hasAuthority('btn.sysRole.delete')")
    @PostMapping("/delete")
    public BaseResponse<Boolean> deleteRole(@RequestBody DeleteRequest deleteRequest) {
        if (deleteRequest == null || deleteRequest.getId() <= 0) {
            throw new BusinessException(CodeEnum.PARAMS_ERROR);
        }
        boolean b = sysRoleService.removeById(deleteRequest.getId());
        return ResultUtils.success(b, "删除成功");
    }

    /**
     * 批量删除角色
     *
     * @param ids
     * @return
     */
    @Log(title = "角色管理", businessType = BusinessType.BATCHDELETE)
    @ApiOperation("批量删除")
    @PostMapping("batchDelete")
    public BaseResponse<Boolean> batchDeleteRole(@RequestBody List<Long> ids) {
        boolean b = sysRoleService.removeByIds(ids);
        return ResultUtils.success(b, "删除成功");
    }

    /**
     * 更新角色
     *
     * @param sysRoleUpdateRequest
     * @return
     */
    @Log(title = "角色管理", businessType = BusinessType.UPDATE)
    @PreAuthorize("hasAuthority('btn.sysRole.update')")
    @PostMapping("/update")
    public BaseResponse<Boolean> updateRole(@RequestBody SysRoleUpdateRequest sysRoleUpdateRequest) {
        if (sysRoleUpdateRequest == null || sysRoleUpdateRequest.getId() == null) {
            throw new BusinessException(CodeEnum.PARAMS_ERROR);
        }
        SysRole sysRole = new SysRole();
        BeanUtils.copyProperties(sysRoleUpdateRequest, sysRole);
        boolean result = sysRoleService.updateById(sysRole);
        return ResultUtils.success(result, "修改成功");
    }

    /**
     * 根据 id 获取角色
     *
     * @param id
     * @return
     */
    @GetMapping("/get")
    public BaseResponse<SysRole> getRoleById(int id) {
        if (id <= 0) {
            throw new BusinessException(CodeEnum.PARAMS_ERROR);
        }
        SysRole sysRole = sysRoleService.getById(id);
        return ResultUtils.success(sysRole);
    }

    /**
     * 获取角色列表
     *
     * @param sysRoleQueryRequest
     * @return
     */
    @PreAuthorize("hasAuthority('btn.sysRole.list')")
    @GetMapping("/list")
    public BaseResponse<List<SysRole>> listRole(SysRoleQueryRequest sysRoleQueryRequest) {
        SysRole sysRole = new SysRole();
        if (sysRoleQueryRequest != null) {
            BeanUtils.copyProperties(sysRoleQueryRequest, sysRole);
        }
        QueryWrapper<SysRole> queryWrapper = new QueryWrapper<>(sysRole);
        List<SysRole> roleList = sysRoleService.list(queryWrapper);
        return ResultUtils.success(roleList);
    }

    /**
     * 分页获取角色列表
     *
     * @param sysRoleQueryRequest
     * @return
     */
    @PreAuthorize("hasAuthority('btn.sysRole.list')")
    @GetMapping("/list/page")
    public BaseResponse<Page<SysRole>> listRoleByPage(SysRoleQueryRequest sysRoleQueryRequest) {
        long current = 1;
        long size = 10;
        SysRole sysRole = new SysRole();
        if (sysRoleQueryRequest != null) {
            BeanUtils.copyProperties(sysRoleQueryRequest, sysRole);
            current = sysRoleQueryRequest.getCurrent();
            size = sysRoleQueryRequest.getPageSize();
        }
        LambdaQueryWrapper<SysRole> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(false, SysRole::getRoleName, sysRoleQueryRequest.getRoleName());
        Page<SysRole> rolePage = sysRoleService.page(new Page<>(current, size), queryWrapper);
        return ResultUtils.success(rolePage);
    }

    // endregion
}
